package org.spbsu.apco.common.monitoring.event;

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicLong;

/**
 * User: Anton Ovchinnikov
 * Date: 1/10/13
 * Time: 3:04 PM
 */
public abstract class BaseEvent implements Serializable, Cloneable {
    protected String eventId;
    protected String originatorId;
    protected String taskId;
    protected String taskType;
    protected long taskStartTime;
    protected long taskDuration;

    private static AtomicLong nextEventId = new AtomicLong(0);

    protected BaseEvent() {
        eventId = "" + nextEventId.incrementAndGet();
    }

    private static String getNewEventId() {
        return nextEventId.getAndIncrement() + "";
    }

    public void generateNewId() {
        eventId = getNewEventId();
    }

    public String getEventId() {
        return eventId;
    }

    public void setEventId(String eventId) {
        this.eventId = eventId;
    }

    public String getOriginatorId() {
        return originatorId;
    }

    public void setOriginatorId(String originatorId) {
        this.originatorId = originatorId;
    }

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public long getTaskStartTime() {
        return taskStartTime;
    }

    public void setTaskStartTime(long taskStartTime) {
        this.taskStartTime = taskStartTime;
    }

    public long getTaskDuration() {
        return taskDuration;
    }

    public void setTaskDuration(long taskDuration) {
        this.taskDuration = taskDuration;
    }

    public String getTaskType() {
        return taskType;
    }

    public void setTaskType(String taskType) {
        this.taskType = taskType;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        BaseEvent baseEvent = (BaseEvent) o;

        if (taskDuration != baseEvent.taskDuration) return false;
        if (taskStartTime != baseEvent.taskStartTime) return false;
        if (!eventId.equals(baseEvent.eventId)) return false;
        if (!originatorId.equals(baseEvent.originatorId)) return false;
        if (taskId!=null && !taskId.equals(baseEvent.taskId)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = eventId.hashCode();
        result = 31 * result + originatorId.hashCode();
        result = 31 * result + (taskId!=null?taskId.hashCode():0);
        result = 31 * result + (int) (taskStartTime ^ (taskStartTime >>> 32));
        result = 31 * result + (int) (taskDuration ^ (taskDuration >>> 32));
        return result;
    }
}
